'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Drawing
Imports ua

Module Module1

    Private AppDataPath As String = GetAppDataPath()

    'configuration data
    Private NewConfigurationData As New ConfigurationData

    'nic data
    Private NicValueDown As New System.Diagnostics.PerformanceCounter
    Private NicValueUp As New System.Diagnostics.PerformanceCounter
    Private NicUsageDown As Double
    Private NicUsageUp As Double

    Sub Main()
        Try
            'already running, exit
            Dim myProcesses As Process() = Process.GetProcessesByName("UAShutdownUpdate")
            If myProcesses.Length > 1 Then End
            myProcesses = Nothing

            If UnixDetected() = True Then
                MessageBox.Show("UAShutdownUpdate is not yet supported under Mono.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End
            End If

            If My.Computer.FileSystem.FileExists(AppDataPath & "usageagent.cfg") = False Then
                'MessageBox.Show("Cannot find " & AppDataPath & "usageagent.cfg. Usage Agent Configuration must be run prior to using this application.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End
            End If

            '############################################################

            'load config
            If LoadConfiguration(NewConfigurationData) = True Then
                'if isp has not been validated, abort
                If NewConfigurationData.ISPValidated = False Then
                    'MsgBox("The connection to your ISP has not been validated. You must validate prior to running Usage Agent. Please re-run UA Configuration.", MsgBoxStyle.Exclamation, "Usage Agent")
                    End
                End If

                If NewConfigurationData.RetrieveNICData = True Then
                    'enable tcp performance if currently disabled
                    RegEnablePerfCounter()

                    Try
                        NicValueDown.CategoryName = "Network Interface"
                        NicValueDown.CounterName = "Bytes Received/sec"
                        NicValueDown.MachineName = "."

                        NicValueUp.CategoryName = "Network Interface"
                        NicValueUp.CounterName = "Bytes Sent/sec"
                        NicValueUp.MachineName = "."

                        'get NIC data
                        NicValueDown.InstanceName = NewConfigurationData.NICName
                        NicValueUp.InstanceName = NewConfigurationData.NICName

                        'set nic
                        NicUsageDown = CDbl(NicValueDown.RawValue)
                        NicUsageUp = CDbl(NicValueUp.RawValue)

                        WriteLog(Nothing, NewConfigurationData.RetrieveNICData, NicUsageDown, NicUsageUp)
                    Catch ex As Exception
                        NewConfigurationData.RetrieveNICData = False
                    End Try
                End If
            End If
        Catch ex As Exception
            'MessageBox.Show(ex.Message, "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
End Module
